package com.example.tkserver.service.impl;

import com.example.tkserver.dao.ArticleDao;
import com.example.tkserver.entity.Article;
import com.example.tkserver.service.interfaces.ArticleService;
import com.example.tkserver.entity.MyException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Log4j2
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleDao articleDao;

    @Override
    public void publishArticle(Article article) {
        log.info("发布吐槽交流:"+article);
        // 将image数组 转为字符串
        article.setImages(Arrays.toString(article.getImageList()));
        if (articleDao.publishArticle(article) == -1){
            throw new MyException("发布失败！请联系管理员进行反馈！");
        }
        log.info("发布吐槽完成");
    }

    @Override
    public int getRecommendArticleNum(){
        log.info("获取推荐吐槽交流数量");
        int num = articleDao.getRecommendArticleNum();
        log.info("获取推荐吐槽交流数量完成"+num);
        return num;
    }

    @Override
    public List<Article> getRecommendArticle(int start,int end) {
        log.info("获取推荐吐槽交流列表:"+start+"-"+end);
        List<Article> result = articleDao.getRecommendArticle(start,end);
        for (Article a:result) {
            String[] images = a.getImages()
                    .substring(1,a.getImages().length()-1)
                    .split(",");
            a.setImageList(images);
        }
        log.info("获取推荐吐槽交流列表完成");
        return result;
    }
}
